<?php

    if(!defined('BASEPATH'))
        exit('No direct script access allowed');

    /**
     * @author NU SIT Bar
     * @version 2.0
     */
    Class Booking_session
    {

        var $_CI;

        function __construct()
        {
            $this->_CI=& get_instance();
            $this->set('source_info', 'www.novisajt.com');

        }

        /**
         * 
         * Create session from Search Module Rentacar
         * 
         * @param $_POST ->
         * @param return_loc_value
         * @param return_loc_text
         * @param return_loc_id
         * @param pickup_loc_value
         * @param pickup_loc_text
         * @param pickup_loc_id
         */
        function step_2($locations)
        {
            if($this->_CI->input->post('pickup_loc_id')):
                foreach($locations as $location):
                    foreach($location as $loc):
                        if($loc->ID == $this->_CI->input->post('pickup_loc_id'))
                        {
                            $this->_CI->session->set_userdata('pickup_loc_id', $loc->ID);
                            $this->_CI->session->set_userdata('pickup_loc_text', $loc->name);
                            $this->_CI->session->set_userdata('pickup_loc_value', $loc->price);
                        }
                        if($loc->ID == $this->_CI->input->post('return_loc_id'))
                        {
                            $this->_CI->session->set_userdata('return_loc_id', $loc->ID);
                            $this->_CI->session->set_userdata('return_loc_text', $loc->name);
                            $this->_CI->session->set_userdata('return_loc_value', $loc->price);
                        }
                    endforeach;
                endforeach;

                $this->_CI->session->set_userdata('fullfrom', $this->_CI->input->post('fullfrom'));
                $this->_CI->session->set_userdata('fullto', $this->_CI->input->post('fullto'));
                $this->_CI->session->set_userdata('pickup_date', $this->_CI->input->post('pickup_date'));
                $this->_CI->session->set_userdata('return_date', $this->_CI->input->post('return_date'));

            endif;
            $this->check_session();

        }

        /*
         * Car details
         * 
         * @param car :: object
         */

        function step_3($car)
        {
            // Check if different car is selected and delete accessories session
            if($this->get('car_id') != $car->id)
            {
                $this->unsetData('extras');
                //$this->_CI->firephp->log('unset extras');
            }

            //set car_id in session
            $this->set('car_id', $car->id);

            //get numberOfDays and set day_price in session
            $no_days=$this->get('no_days');
            switch($no_days)
            {
                case $no_days <= 3:
                    $this->set('day_price', $car->day13price);
                    break;

                case $no_days >= 4 && $no_days <= 7:
                    $this->set('day_price', $car->day47price);
                    break;

                case $no_days >= 8 && $no_days <= 15:
                    $this->set('day_price', $car->day815price);
                    break;

                case $no_days >= 15:
                    $this->set('day_price', $car->dayfrom15price);
                    break;
            }

        }

        /*
         * Set extras in session, if nothing is pased, set empty array in session
         * 
         * @param $extras
         */

        function extras($extrasPost=FALSE)
        {
            $extras=array();

            if($extrasPost)
            {

                //$this->_CI->firephp->log('setArryExtras');

                foreach($extrasPost as $key => $value)
                {
                    $extras[]=$key;
                }
            }

            $this->set('extras', $extras);

        }

        /*
         * Check if first session data is set / redirect if False 
         */

        function check_session()
        {

            if(!$this->_CI->session->userdata('pickup_loc_id'))
                redirect(base_url());

        }

        /*
         * Get / Set / unsetData / GetAll method wrapper for session
         */

        function get($key)
        {
            return $this->_CI->session->userdata($key);

        }

        function set($key, $value)
        {
            return $this->_CI->session->set_userdata($key, $value);

        }

        function unsetData($key)
        {
            $this->_CI->session->unset_userdata($key);

        }

        function get_all_data()
        {

            return $this->_CI->session->all_userdata();

        }

        function get_all_data_test()
        {

            $data=$this->_CI->session->all_userdata();
            unset($data['session_id']);
            unset($data['ip_address']);
            unset($data['user_agent']);
            unset($data['last_activity']);
            unset($data['user_data']);
            return $data;

        }

    }

?>
